package by.gravity.common.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;

public class CommonFragment extends Fragment {

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		IntentFilter filter = new IntentFilter();
		addIntentFilter(filter);
		registerReceiver(filter);
	}

	protected void addIntentFilter(IntentFilter filter) {

	}

	protected void sendBroadcast(Intent intent) {
		LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);
	}

	private void registerReceiver(IntentFilter filter) {
		if (filter.countActions() > 0) {
			BroadcastReceiver receiver = new BroadcastReceiver() {

				@Override
				public void onReceive(Context context, Intent intent) {
					onBroadcastReceive(intent,intent.getAction());
				}
			};
			LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, filter);
		}
	}

	protected void onBroadcastReceive(Intent intent,String intentAction) {

	}

}
